<?php


class Post extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('Post_model');
		$this->load->model('PostContent_model');
		$this->load->helper('url');
		$this->load->model('Category_model');
	}

	public function main()
	{
		$this->load->library('pagination'); // 导入翻页类

		$config = array(
			'base_url' => '/bm/post/main', // 分页url
			'total_rows' => $this->db->count_all('post'), // 统计总数
			'per_page' => 10, // 每页显示的文件
			'use_page_numbers' => true,
			'first_link' => '首页',
			'last_link' => '末页',
			'num_links' => 5,
			'cur_tag_open' =>'<a data-ci-pagination-page>',
			'cur_tag_close' =>'</a>',
			'num_links' => 2
		);

		$data = array(
			'list' => $this->Post_model->lists($config['per_page'], $this->uri->segment(4)),
		);
		$this->pagination->initialize($config);

		$this->load->view('bm/post/main', $data);
	}

	public function create()
	{
		if ($_POST) {
			// 基础数据
			$post = array(
				'title' => $this->input->post('title'),
				'keyword' => $this->input->post('keyword'),
				'description' => $this->input->post('description'),
				'images' => $this->input->post('images'),
				'category_id' => $this->input->post('category_id'),
				'create_time' => getTimeStamp('Y-m-d H:i:s.u'),
				'update_time' => getTimeStamp('Y-m-d H:i:s.u'),
			);
			// 基础数据添加,并且启动事务
			$this->db->trans_start();
			$postId = $this->Post_model->insert($post);
			if ($postId > 0) {
				// 内容添加
				$content = array(
					'content' => $this->input->post('content'),
					'post_id' => $postId
				);
				$result = $this->PostContent_model->insert($content);
				$this->db->trans_complete();
				if ($result) {
					$msg = array(
						'message' => '添加成功',
						'waitSecond' => 1,
						'jumpUrl' => '/bm/post/main'
					);
					$this->load->view('message/success', $msg);
				}
			}
		} else {
			// 获取数组
			$categoryList = $this->Category_model->lists();
			// 转换格式
			$tree_to_select = tree_to_select($categoryList);
			// 赋值
			$data = array(
				'tree_to_select' => $tree_to_select
			);

			$this->load->view('bm/post/create', $data);
		}
	}

	public function update()
	{
		if ($_POST) {

			$id = $this->input->post('id');
			// 基础数据
			$post = array(
				'title' => $this->input->post('title'),
				'keyword' => $this->input->post('keyword'),
				'description' => $this->input->post('description'),
				'images' => $this->input->post('images'),
				'category_id' => $this->input->post('category_id'),
				'update_time' => getTimeStamp('Y-m-d H:i:s.u'),
			);
			// 基础数据修改
			$result = $this->Post_model->update($id, $post);
			if ($result) {
				// 内容添加
				$content = array(
					'content' => $this->input->post('content')
				);
				$result = $this->PostContent_model->update($id, $content);
				if ($result) {
					$msg = array(
						'message' => '修改成功',
						'waitSecond' => 1,
						'jumpUrl' => '/bm/post/main'
					);
					$this->load->view('message/success', $msg);
				}
			}
		} else {
			// 获取数据
			$id = $this->input->get('id') ?? $this->uri->segment(5);
			$info = $this->Post_model->info($id);
			// 获取数组
			$categoryList = $this->Category_model->lists();
			// 转换格式
			$tree_to_select = tree_to_select($categoryList);
			// 赋值
			$data = array(
				'info' => $info,
				'tree_to_select' => $tree_to_select
			);
			$this->load->view('bm/post/update', $data);
		}
	}

	public function view()
	{

	}

	public function delete()
	{
		$id = $this->input->get('id');

		if ($id) {
			// 删除基础表中的数据
			$result = $this->Post_model->del($id);
			if ($result) {
				// 删除内容表中的数据
				$result = $this->PostContent_model->del($id);
				if ($result) {
					$msg = array(
						'message' => '删除成功',
						'waitSecond' => 1,
						'jumpUrl' => '/bm/post/index'
					);
				}

			} else {
				$msg = array(
					'message' => '删除失败，请核对',
					'waitSecond' => 1,
					'jumpUrl' => '/bm/post/index'
				);
			}
		} else {
			$msg = array(
				'message' => '参数非法',
				'waitSecond' => 1,
				'jumpUrl' => '/bm/post/index'
			);
		}

		$this->load->view('message/success', $msg);
	}
}
